package com.lzq.chatroom_project.common.interceptor;

import com.lzq.chatroom_project.common.pojo.Result;
import com.lzq.chatroom_project.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String jwtToken = request.getHeader("user_token");
        log.info("从header中获取token:{}",jwtToken);
        Result result = JwtUtil.parseJwt(jwtToken);
        log.info("获取响应结果:{}",result);
        Claims claims = (Claims) result.getData();
        if (claims!=null) {
            log.info("令牌通过");
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
